package com.duplicate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CountDuplicatedList {
public static void main(String[] args) {
	List<String > list=new ArrayList<String>();
	list.add("deepak");
	list.add("kumar");
	list.add("deepak");
	list.add("goswami");
	list.add("dk");
	list.add("goswami");
	list.add("deepak");
	System.out.println("Example-1 count 'deepak' with frequency : "+Collections.frequency(list, "a"));
	System.out.println("Example-2 count all with frequency");
	Set<String> uniqueSet=new HashSet<String>(list);
	for(String temp:uniqueSet){
		System.out.println(temp+" : "+Collections.frequency(list, temp));
	}
	System.out.println("Example-3 count all with map");
	Map<String, Integer> map=new HashMap<String, Integer>();
	for(String temp:list){
		Integer count=map.get(temp);
		map.put(temp, (count==null)?1:count+1);
	}
	//printMap(map);
	for(Map.Entry<String, Integer> entry:map.entrySet()){
		System.out.println("Key : "+entry.getKey()+"\tValue : "+entry.getValue());
	}
}
public static void printMap(Map<String, Integer> map){
	for(Map.Entry<String, Integer> entry:map.entrySet()){
		System.out.println("Key : "+entry.getKey()+"\tValue : "+entry.getValue());
	}
}
}
